package queue

import (
	"sync"
)

type QueueData struct {
	mu   sync.Mutex
	data []interface{}
}

func NewQueue() *QueueData {
	return &QueueData{
		data: make([]interface{}, 0),
	}
}

// Enqueue pushes data into the queue
func (q *QueueData) Push(value interface{}) {
	q.mu.Lock()
	defer q.mu.Unlock()
	q.data = append(q.data, value)
}

// Dequeue retrieves and removes the first element from the queue
func (q *QueueData) Pull() (interface{}, bool) {
	q.mu.Lock()
	defer q.mu.Unlock()
	if len(q.data) == 0 {
		return nil, false
	}
	value := q.data[0]
	q.data = q.data[1:]
	return value, true
}

// GetAll retrieves all elements from the queue and clears it
func (q *QueueData) GetAll() []interface{} {
	q.mu.Lock()
	defer q.mu.Unlock()
	allData := q.data
	q.data = make([]interface{}, 0)
	return allData
}

// Length returns the number of elements in the queue
func (q *QueueData) Length() int {
	q.mu.Lock()
	defer q.mu.Unlock()
	return len(q.data)
}

// Clear clears all elements from the queue
func (q *QueueData) Clear() {
	q.mu.Lock()
	defer q.mu.Unlock()
	q.data = make([]interface{}, 0)
}
